package org.example.node;

/**
 * @author liyishan
 * @date 2024/7/4 09:32
 * @apiNote
 */

public class Test00876MiddleNode {

    public ListNode middleNode(ListNode head) {
        ListNode[] A = new ListNode[100];
        int t =0;
        while(head != null){
            A[t++] = head;
            head = head.next;
        }
        return A[t/2];
    }

    public ListNode middleNode1(ListNode head) {
        int n =0;
        ListNode cur = head;
        while(cur != null){
            n++;
            cur = cur.next;
        }
        int k = 0;
        cur = head;
        while(k <n/2){
            k++;
            cur = cur.next;
        }
        return cur;
    }

    public ListNode middleNode2(ListNode head) {
        ListNode slow = head,fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
